Class SparseMatrix


  • public class SparseMatrix
    extends java.lang.Object
    SparseMatrix is a sparse matrix with row format.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      java.util.HashMap<java.lang.Integer,​java.util.PriorityQueue<HeapNode>> heaps  
      java.util.HashMap<java.lang.Integer,​BinarySearchTree> trees  
    • Constructor Summary

      Constructors 
      Constructor Description
      SparseMatrix​(int rows)
      empty sparse matrix with allocated number of rows
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int i, int j, double q)
      Add the given j index, q value pair to the ith tree and heap
      void addHeap​(int i, int j, double q)
      Add the given j index, q value pair to the ith heap.
      void addTree​(int i, int j, double q)
      Add the given j index, q value pair to the ith tree.
      java.lang.Double get​(int i, int j)
      get number at index or null if not set.
      HeapNode getMax​(int i)
      Get the maximum q value at row i
      void mergeTrees​(int j, int i)
      Merge Trees j and i into j, summing values if the same column occurs.
      void rebuildHeap​(int j)
      Rebuild heap j with the tree j
      void remove​(int i)
      Remove the ith row
      private void removeFromTree​(int i, int j)  
      void set​(int i, int j, double q)
      Set the q value at the ith tree and heap's j column.
      void setHeap​(int i, int j, double q)
      Set the value q and the ith heap's j column.
      void setTree​(int i, int j, double q)
      Set value q and the ith tree's j column.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • heaps

        public java.util.HashMap<java.lang.Integer,​java.util.PriorityQueue<HeapNode>> heaps
    • Constructor Detail

      • SparseMatrix

        public SparseMatrix​(int rows)
        empty sparse matrix with allocated number of rows
        Parameters:
        rows - the number of rows
    • Method Detail

      • add

        public void add​(int i,
                        int j,
                        double q)
        Add the given j index, q value pair to the ith tree and heap
        Parameters:
        i - the row to add
        j - the j index
        q - the q value
      • addTree

        public void addTree​(int i,
                            int j,
                            double q)
        Add the given j index, q value pair to the ith tree.
        Parameters:
        i - the row to add
        j - the j index
        q - the q value
      • addHeap

        public void addHeap​(int i,
                            int j,
                            double q)
        Add the given j index, q value pair to the ith heap.
        Parameters:
        i - the row to add
        j - the j index
        q - the q value
      • get

        public java.lang.Double get​(int i,
                                    int j)
        get number at index or null if not set.
        Parameters:
        i - the row to search
        j - the colum to search
        Returns:
        the q value if it exists or null
      • setTree

        public void setTree​(int i,
                            int j,
                            double q)
        Set value q and the ith tree's j column. add it if it's not there.
        Parameters:
        i - the row to set in
        j - the column to set in
        q - the new q value
      • setHeap

        public void setHeap​(int i,
                            int j,
                            double q)
        Set the value q and the ith heap's j column. add it if it's not there.
        Parameters:
        i - the row to set in
        j - the column to set in
        q - the new q value
      • set

        public void set​(int i,
                        int j,
                        double q)
        Set the q value at the ith tree and heap's j column.
        Parameters:
        i - the row to set in
        j - the column to set in
        q - the new q value
      • removeFromTree

        private void removeFromTree​(int i,
                                    int j)
      • getMax

        public HeapNode getMax​(int i)
        Get the maximum q value at row i
        Parameters:
        i - the row to search in
        Returns:
        the maximum q value
      • remove

        public void remove​(int i)
        Remove the ith row
        Parameters:
        i - the row to clear in
      • mergeTrees

        public void mergeTrees​(int j,
                               int i)
        Merge Trees j and i into j, summing values if the same column occurs.
        Parameters:
        j - the tree to expand
        i - the tree that offers its Nodes
      • rebuildHeap

        public void rebuildHeap​(int j)
        Rebuild heap j with the tree j
        Parameters:
        j - the heap to rebuild
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object